package cn.south.campus.admin.sys.service.impl;

import cn.south.campus.admin.config.TokenConfig;
import cn.south.campus.admin.sys.mapper.SysUserMapper;
import cn.south.campus.admin.sys.model.bo.LoginContext;
import cn.south.campus.admin.sys.model.bo.LoginInfo;
import cn.south.campus.admin.sys.model.entity.SysRole;
import cn.south.campus.admin.sys.model.entity.SysUser;
import cn.south.campus.admin.sys.model.req.SysUserReq;
import cn.south.campus.admin.sys.model.vo.LoginVO;
import cn.south.campus.admin.sys.model.vo.SysUserVo;
import cn.south.campus.admin.sys.service.SysRoleService;
import cn.south.campus.admin.sys.service.SysUserService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import cn.south.campus.common.utils.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 用户表(SysUser)表服务实现类
 *
 * @author chow
 * @since 2024/10/31
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public IPage<SysUserVo> sysUserPage(SysUserReq req) {
        IPage<SysUser> userPage = sysUserMapper.selectPageFill(req.getPageDto(), req);
        return userPage.convert(SysUserVo::convert);
    }


    @Override
    public SysUserVo selectByMobileAndPwd(String mobile, String password) {
        String encryptPwd = MD5Utils.MD5(TokenConfig.SECRET + password);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getMobile, mobile).eq(SysUser::getPassword, encryptPwd);
        return SysUserVo.convert(sysUserMapper.selectOne(wrapper));
    }

    @Override
    public void add(SysUserVo sysUserVo) {
        SysRole role = sysRoleService.getById(sysUserVo.getRoleId());
        AssertUtils.isNotNull(role, CommonEnums.DATA_HAS_NOT_ERROR);
        // 手机号排除
        Long count = sysUserMapper.selectCount(new QueryWrapper<SysUser>().lambda().select(SysUser::getId)
                .eq(SysUser::getMobile, sysUserVo.getMobile()));
        AssertUtils.isLtE(count, 1L, CommonEnums.DATA_REPEAT_ERROR);
        SysUser sysUser = SysUserVo.convert(sysUserVo);
        sysUser.setPassword(MD5Utils.MD5(TokenConfig.SECRET + 123456));
        sysUser.setDeptId(1L);
        sysUserMapper.insert(sysUser);
    }

    @Override
    public void delete(Long id) {
        SysUser sysUser = getById(id);
        AssertUtils.isNotNull(sysUser, CommonEnums.DATA_HAS_NOT_ERROR);
        sysUserMapper.deleteById(id);
    }

    @Override
    public void updateSysUser(SysUserVo sysUserVo) {
        SysUser user = getById(sysUserVo.getId());
        AssertUtils.isNotNull(user, CommonEnums.DATA_HAS_NOT_ERROR);
        SysRole role = sysRoleService.getById(sysUserVo.getRoleId());
        AssertUtils.isNotNull(role, CommonEnums.DATA_HAS_NOT_ERROR);
        user.setUsername(sysUserVo.getUsername());
        user.setDeptId(sysUserVo.getDeptId());
        user.setRoleId(sysUserVo.getRoleId());
        user.setLocked(sysUserVo.getLocked());
        user.setAvatar(sysUserVo.getAvatar());
        user.setEmail(sysUserVo.getEmail());
        sysUserMapper.updateById(user);
    }

    @Override
    public SysUserVo detail(Long id) {
        SysUser sysUser = getById(id);
        return SysUserVo.convert(sysUser);
    }

    @Override
    public List<SysUser> queryByDeptId(Long id) {
        return sysUserMapper.selectList(new QueryWrapper<SysUser>().lambda().eq(SysUser::getDeptId, id));
    }

    @Override
    public void modifyMyInfo(LoginVO vo) {
        LoginInfo context = LoginContext.getContext();
        AssertUtils.isEquals(context.getId(), vo.getId(), CommonEnums.SYSTEM_ERROR);
        SysUser user = getById(context.getId());
        AssertUtils.isNotNull(user, CommonEnums.DATA_HAS_NOT_ERROR);
        user.setUsername(vo.getUsername());
        user.setAvatar(vo.getAvatar());
        user.setEmail(vo.getEmail());
        if (vo.getPassword() != null) {
            user.setPassword(MD5Utils.MD5(TokenConfig.SECRET + vo.getPassword()));
        }
        user.setMobile(vo.getMobile());
        sysUserMapper.updateById(user);
    }
}
